package org.jeecg.modules.wms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wms.entity.WmsRequisitionAttachment;
import org.jeecg.modules.wms.service.IWmsRequisitionAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 领料单附件控制器
 */
@Slf4j
@Api(tags = "领料单附件管理")
@RestController
@RequestMapping("/wms/requisition/attachment")
public class WmsRequisitionAttachmentController extends JeecgController<WmsRequisitionAttachment, IWmsRequisitionAttachmentService> {

    @Autowired
    private IWmsRequisitionAttachmentService wmsRequisitionAttachmentService;

    /**
     * 上传附件
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传领料单附件", notes = "上传领料单附件")
    @AutoLog(value = "上传领料单附件", operateType = 2)
    public Result<WmsRequisitionAttachment> upload(@RequestParam("requisitionId") String requisitionId,
                                                  @RequestParam("file") MultipartFile file,
                                                  HttpServletRequest request) {
        try {
            // 从请求中获取用户信息 - 尝试多种方式获取token
            String token = request.getHeader("X-Access-Token");
            if (StringUtils.isBlank(token)) {
                token = request.getParameter("token");
            }
            
            // 记录请求信息用于调试
            log.info("上传附件请求参数: requisitionId={}, token存在={}, 请求URI={}, 请求IP={}", 
                     requisitionId, 
                     (token != null), 
                     request.getRequestURI(),
                     oConvertUtils.getIpAddrByRequest(request));
            
            String username = null;
            if (StringUtils.isNotBlank(token)) {
                try {
                    username = JwtUtil.getUsername(token);
                } catch (Exception e) {
                    log.error("解析token失败", e);
                }
            }
            
            if (StringUtils.isBlank(username)) {
                // 尝试从请求参数中获取用户名
                username = request.getParameter("uploader");
                if (StringUtils.isBlank(username)) {
                    // 如果还是获取不到，使用默认用户名
                    username = "system";
                    log.warn("未能从token获取用户信息，使用默认用户名: {}", username);
                }
            }
            
            return wmsRequisitionAttachmentService.uploadAttachment(requisitionId, file, username);
        } catch (Exception e) {
            log.error("上传附件失败", e);
            return Result.error("上传附件失败: " + e.getMessage());
        }
    }

    /**
     * 删除附件
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除领料单附件", notes = "删除领料单附件")
    @AutoLog(value = "删除领料单附件", operateType = 3)
    public Result<Boolean> delete(@RequestParam("id") String id) {
        return wmsRequisitionAttachmentService.deleteAttachment(id);
    }

    /**
     * 获取附件列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取领料单附件列表", notes = "根据领料单ID获取附件列表")
    @AutoLog(value = "获取领料单附件列表", operateType = 1)
    public Result<List<WmsRequisitionAttachment>> getAttachmentList(@RequestParam("requisitionId") String requisitionId) {
        List<WmsRequisitionAttachment> list = wmsRequisitionAttachmentService.getAttachmentsByRequisitionId(requisitionId);
        return Result.OK(list);
    }
} 